﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebUI.DAL;
using System.Data.Linq;

namespace WebUI.BL
{
    public class ProductPricePlanBL
    {
        public void Insert(tbl_ProductPricePlan productPrice)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_ProductPricePlan newProductPrice = new tbl_ProductPricePlan()
                    {
                        ToDate=productPrice.ToDate,
                        Status=productPrice.Status,
                        SalePrice=productPrice.SalePrice,
                        ProductNo=productPrice.ProductNo,
                        OriginalPrice=productPrice.OriginalPrice,
                        Note=productPrice.Note,
                        MarketPrice=productPrice.MarketPrice,
                        MarginPrice=productPrice.MarginPrice,
                        FromDate=productPrice.FromDate,
                        DiscountPrice=productPrice.DiscountPrice,
                        DiscountPercent=productPrice.DiscountPercent,
                        CreatedUser=productPrice.CreatedUser,
                        CreatedDate=productPrice.CreatedDate
                    };
                    contextdb.tbl_ProductPricePlans.InsertOnSubmit(newProductPrice);
                    contextdb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void Update(tbl_ProductPricePlan productPrice)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var updateproductPrice = contextdb.tbl_ProductPricePlans.SingleOrDefault(u => u.PlanId == productPrice.PlanId);
                try
                {
                    if (updateproductPrice != null)
                    {
                        updateproductPrice.ToDate = productPrice.ToDate;
                        updateproductPrice.Status = productPrice.Status;
                        updateproductPrice.SalePrice = productPrice.SalePrice;
                        updateproductPrice.ProductNo = productPrice.ProductNo;
                        updateproductPrice.OriginalPrice = productPrice.OriginalPrice;
                        updateproductPrice.Note = productPrice.Note;
                        updateproductPrice.MarketPrice = productPrice.MarketPrice;
                        updateproductPrice.MarginPrice = productPrice.MarginPrice;
                        updateproductPrice.FromDate = productPrice.FromDate;
                        updateproductPrice.DiscountPrice = productPrice.DiscountPrice;
                        updateproductPrice.DiscountPercent = productPrice.DiscountPercent;
                        updateproductPrice.CreatedUser = productPrice.CreatedUser;
                        updateproductPrice.CreatedDate = productPrice.CreatedDate;

                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }

            }
        }
        public void Delete(int pricePlanId)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteRelateNo = contextdb.tbl_ProductPricePlans.SingleOrDefault(u => u.PlanId == pricePlanId);
                try
                {
                    if (deleteRelateNo != null)
                    {
                        contextdb.tbl_ProductPricePlans.DeleteOnSubmit(deleteRelateNo);
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void DeleteByStatus(int pricePlanId)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteRelateNo = contextdb.tbl_ProductPricePlans.SingleOrDefault(u => u.PlanId == pricePlanId);
                try
                {
                    if (deleteRelateNo != null)
                    {
                        deleteRelateNo.Status = "Deleted";
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
    }
}
